<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <link href="/assets/accessibility.liveRegionAttachment.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="https://unpkg.com/@babel/standalone@7.8.7/babel.min.js"></script>
    <script crossorigin="anonymous" src="https://unpkg.com/react@16.8.6/umd/react.production.min.js"></script>
    <script crossorigin="anonymous" src="https://unpkg.com/react-dom@16.8.6/umd/react-dom.production.min.js"></script>
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
    <script crossorigin="anonymous" src="/__dist__/botframework-webchat-fluent-theme.production.min.js"></script>
    <style>
      .sendbox__error-message {
        all: unset !important;
        bottom: 100px !important;
        color: darkred !important;
        margin: 5px !important;
        position: absolute !important;
      }
      .webchat__connectivityStatus > div {
        background-image: none !important;
      }
    </style>
  </head>
  <body>
    <main id="webchat"></main>
    <script type="text/babel">
      run(async function () {
        const {
          React,
          ReactDOM: { render },
          WebChat: { FluentThemeProvider, ReactWebChat }
        } = window;

        const clock = lolex.createClock();

        const { directLine, store } = testHelpers.createDirectLineEmulator({ autoConnect: false, ponyfill: clock });

        // We don't set the connectivityStatus to 'connected' here as we want to test the connecting state

        const App = () => (
          <ReactWebChat
            directLine={directLine}
            store={store}
            styleOptions={{
              internalLiveRegionFadeAfter: 10000
            }}
            ponyfill={clock}
          />
        );

        render(
          <FluentThemeProvider>
            <App />
          </FluentThemeProvider>,
          document.getElementById('webchat')
        );

        await pageConditions.webChatRendered();

        // GIVEN: Connection is still establishing.
        expect(document.querySelector('.webchat__connectivityStatus').innerText).toBe('Connecting…');
        await host.snapshot();

        const errorMessage = document.querySelector('.sendbox__error-message');

        // GIVEN: "Hello, World!" is in the send box.
        await host.click(document.querySelector(`[data-testid="${WebChat.testIds.sendBoxTextBox}"]`));
        await host.sendKeys('Hello, World!');

        // WHEN: Send button is clicked.
        await host.click(document.querySelector(`[data-testid="${WebChat.testIds.sendBoxSendButton}"]`));

        // THEN: The message "Unable to connect." should appear.
        expect(errorMessage.innerText).toBe('Unable to connect.');
        await host.snapshot();

        // THEN: After 10000 ms, the error message should still be visible.
        clock.tick(10000);
        expect(errorMessage.innerText).toBe('Unable to connect.');
        await host.snapshot();

        // WHEN: Send button is clicked again.
        await host.click(document.querySelector(`[data-testid="${WebChat.testIds.sendBoxSendButton}"]`));

        // THEN: The error message should still be "Unable to connect."
        expect(errorMessage.innerText).toBe('Unable to connect.');
        await host.snapshot();
      });
    </script>
  </body>
</html>